package com.huan.loadbalancer;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 将version请求头通过feign传递到下游
 *
 * @author huan.fu
 * @date 2023/6/20 - 08:27
 */
@Component
@Slf4j
public class VersionRequestInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        String version = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()
                .getHeader("version");
        log.info("feign 中传递的 version 请求头的值为:[{}]", version);
        requestTemplate
                .header("version", version);
    }
}
